Variables
When AppleScript encounters a variable in a script, it evaluates the variable by getting its value. To create a variable, simply assign it a value:
copy "Mitch" to myNameThe Copy command takes the data--the string"Mitch"
--and puts it in the variablemyName
. You can accomplish the same thing with the Set command:
set myName to "Mitch"Statements that assign values to variables are known as assignment statements.You can retrieve the value in a variable with a Get command. Run the following script and then display the result:
set myName to "Mitch"get myNameYou see that the value inmyName
is the value you stored with the Set command.You can change the value of a variable by assigning it a new value. A variable can hold only one value at a time. When you assign a new value to an existing variable, you lose the old value. For example, the result of the Get command in the following script is
"Pegi"
.
set myName to "Mitch"set myName to "Pegi"get myNameAppleScript does not distinguish uppercase letters from lowercase variables in variable names; the variablesmyName
,myname
, andMYNAME
all represent the same value.